<template>
  <div class="setting-body">
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <div class="custom-tips">
        基础设置
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <el-col :span="12">
          <el-form-item
            label="最大标签数"
            prop="size"
            :rules="[{ required: true, message: '最大标签数', trigger: 'blur' }]"
          >
            <el-input-number v-model="form.size" :min="1" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="custom-tips">
        标签项设置
        <div class="button-add">
          <el-button type="text" icon="el-icon-plus" @click="doAdd">新增</el-button>
        </div>
      </div>
      <el-row :gutter="20" style="margin-top: 10px">
        <draggable v-model="form.data" animation="300" @end="clone">
          <el-col v-for="(item,index) in form.data" :key="'data-'+index" :span="24">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item
                  label="显示名称"
                  :prop="'data.'+index+'.label'"
                  :rules="[{ required: true, message: '请输入显示名称', trigger: 'blur' }]"
                >
                  <el-input v-model="item.label" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="关联模版"
                  :prop="'data.'+index+'.temp'"
                  :rules="[{ required: true, message: '请选择关联模版', trigger: 'change' }]"
                >
                  <el-select v-model="item.temp" clearable filterable placeholder="请选择关联模版">
                    <el-option
                      v-for="obj in jxx"
                      :key="obj.id"
                      :label="obj.name"
                      :value="obj.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item
                  label="是否显示更多"
                  :prop="'data.'+index+'.isMore'"
                  :rules="{ required: true, message: '请选择是否显示更多', trigger: 'change' }"
                  style="width: 100%"
                >
                  <el-radio-group v-model="item.isMore">
                    <el-radio
                      v-for="item in status"
                      :key="item.id"
                      :label="item.value"
                    >{{ item.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col v-if="item.isMore === true" :span="12">
                <el-form-item
                  label="打开方式"
                  :prop="'data.'+index+'.dkfs'"
                  :rules="{ required: true, message: '请选择打开方式', trigger: 'change' }"
                  style="width: 100%"
                >
                  <el-radio-group v-model="item.dkfs">
                    <el-radio
                      v-for="item in openType"
                      :key="item.id"
                      :label="item.value"
                    >{{ item.label }}
                    </el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col v-if="item.isMore === true && item.dkfs === 2" :span="24">
                <el-form-item
                  label="打开地址"
                  :prop="'data.'+index+'.path'"
                  :rules="{ required: true, message: '请输入打开地址', trigger: 'blur' }"
                  style="width: 100%"
                >
                  <el-input v-model="item.path" />
                </el-form-item>
              </el-col>
              <el-col :span="4" style="text-align: center">
                <el-button type="text" icon="el-icon-delete" size="mini" @click="toDelete(item)">删除</el-button>
              </el-col>
            </el-row>
          </el-col>
        </draggable>
      </el-row>
    </el-form>
    <div class="custom-button">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </div>
</template>
<script>
import { updateObject, queryAllByFl } from '@/api/portal/manager/columnTemp';
import draggable from 'vuedraggable';
export default {
  name: 'PortalV4PcTabsTempV1',
  components: { draggable },
  props: {
    data: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      openType: [
        { id: 0, value: 1, label: '固定模版打开' },
        { id: 1, value: 2, label: '固定地址打开' }
      ],
      status: [
        { id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }
      ],
      orders: [
        { id: 0, value: 'DESC', label: '升序' },
        { id: 1, value: 'ASC', label: '降序' }
      ],
      jxx: [],
      form: {
        isOpenMore: true, // 是否显示更多
        size: 5, // 每页显示几条
        data: []
      }
    };
  },
  watch: {
    data: {
      handler(newName, oldName) {
        if (undefined !== newName && newName.sjlx !== null) {
          if (undefined !== newName.pzx && newName.pzx !== '' && newName.pzx !== null) {
            this.form = JSON.parse(newName.pzx);
          }
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  created() {
    // 解决火狐拖拽搜索的问题
    document.body.ondrop = function(event) {
      event.preventDefault();
      event.stopPropagation();
    };
    this.initTemp();
  },
  methods: {
    save() {
      this.loading = true;
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.data.pzx = JSON.stringify(this.form);
          updateObject(this.data).then(res => {
            this.close();
          }).catch(() => {
            this.loading = false;
          });
        } else {
          this.loading = false;
        }
      });
    },
    doAdd() {
      const px = this.form.data.length + 1;
      const item = { label: '', temp: '', px: px };
      this.form.data.push(item);
    },
    toDelete(item) {
      const index = this.form.data.indexOf(item);
      if (index !== -1) {
        this.form.data.splice(index, 1);
      }
    },
    clone() {
      this.form.data = this.form.data.map((item, index) => {
        item.px = index + 1;
        return item;
      });
    },
    close() {
      this.loading = false;
      Object.assign(this.$data.form, this.$options.data.call(this).form);
      this.dialogFormVisible = false;
      this.$emit('fetch-data');
    },
    initTemp() {
      queryAllByFl({ mbdl: 'TabsTemplate' }).then(res => {
        const { data } = res;
        this.jxx = data.data;
      }).catch(() => {
        this.jxx = [];
      });
    }
  }
};
</script>
<style scoped>
.setting-body {
  width: 100%;
}

.custom-tips {
  width: 100%;
  min-height: 40px;
  line-height: 40px;
  text-align: left;
  padding: 8px 16px;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
}

.button-add {
  width: 50px;
  height: 40px;
  float: right;
  text-align: center;
  line-height: 40px;
  margin-right: 20px;
}
.custom-button {
  width: 100%;
  height: 40px;
  line-height: 40px;
  text-align: right;
}
</style>
